package com.zhongxi.guoyuan.view.rewardCoin.categoryPage;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.Toast;

import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhy.http.okhttp.OkHttpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

/**
 * Created by Administrator on 2017/9/26.
 */

public abstract class BasePage {
    public Context ctx;
    public int pageNum = 1;
    private ProgressDialog progressDialog;

    public BasePage(Context ctx){
        this.ctx=ctx;
    }

    public abstract View initView();

    public abstract void initData();

    public abstract void initEvent();

    class StringCallback extends com.zhy.http.okhttp.callback.StringCallback {

        @Override
        public void onError(Call call, Exception e, int id) {
            try {
                System.out.println("e.getMessage()==" + e.getMessage());
                processFailRequest(e.getMessage());
            } finally {
                hideProgressDialog();
            }
        }

        @Override
        public void onResponse(String response, int id) {
            try {
                JSONObject obj = new JSONObject(response);
                int code = obj.getInt("code");
                if (code == 1) {
                    processSuccessResult(response);
                } else if (code == 7002) {
                    processOldUser(response);
                } else {
                    ProcessFailResult(response);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            } finally {
                hideProgressDialog();
            }
        }
    }

    protected void processFailRequest(String message) {}

    protected void processOldUser(String response) {}

    /**
     * 请求失败
     */
    protected void ProcessFailResult(String response) {}

    /**
     * 请求成功
     */
    protected void processSuccessResult(String response) {

    }


    /**
     * 获取网络请求 map -- 传递参数
     * @param
     * @param getStr
     */
    public void getServer(String url, Map<String, String> map, String getStr, boolean isUser, StringCallback callback, String type){
        try {
            if (map == null) {
                map = new HashMap<>();
            }
            if(callback == null){
                callback = new StringCallback();
            }
            String baseUrl = url +  MyApplication.getInstance().getUrl()+ MyApplication.getInstance().getCheck(isUser);
            if ("get".equals(type)) {
                System.out.println("baseUrl+getStr=="+baseUrl+getStr);
                getHttpUtils(baseUrl + getStr, callback);
            } else {
                postHttpUtils(baseUrl + getStr, callback, map);
            }
            showProgressDialog(type);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    private void getHttpUtils(String url, StringCallback callback) {
        OkHttpUtils
                .get()
                .url(url)
                .build()
                .execute(callback);
    }
    private void postHttpUtils(String url, StringCallback callback, Map<String, String> map) {
        OkHttpUtils
                .post()
                .url(url)
                .params(map)
                .build()
                .execute(callback);
    }
    /**
     * 展示提交或者加载数据的进度框
     * @param method
     */
    private void showProgressDialog(String method) {
        if(progressDialog == null)
            progressDialog = new ProgressDialog(ctx, ProgressDialog.THEME_HOLO_LIGHT);
        progressDialog.setCancelable(true);
        String progressDialogMessage = "";
        if(!progressDialog.isShowing()){
            if(method.equals("get"))
                progressDialogMessage = ctx.getResources().getString(R.string.get);
            else if(method.equals("upload"))
                progressDialogMessage = ctx.getResources().getString(R.string.upload);
            else
                progressDialogMessage = "正在请求数据……";
            progressDialog.setMessage(progressDialogMessage);
            progressDialog.show();
        }
    }

    /**
     * 隐藏对话框
     */
    private void hideProgressDialog() {
        if(progressDialog != null && progressDialog.isShowing()){
            progressDialog.dismiss();
            progressDialog.cancel();
        }
    }

    public void startAct(Class clazz){
        Intent intent = new Intent(ctx, clazz);
        ctx.startActivity(intent);
    }
    public void showShortToast(String message) {
        Toast.makeText(ctx, message, Toast.LENGTH_SHORT).show();
    }
}
